package com.booth.base;

import com.alibaba.fastjson.JSON;
import com.booth.base.controllers.DataDictController;
import com.booth.base.controllers.GroupsController;
import com.booth.base.controllers.GroupsModulesController;
import com.booth.base.controllers.MainController;
import com.booth.base.controllers.ModuleActionsController;
import com.booth.base.controllers.ModulesController;
import com.booth.base.controllers.UsersController;
import com.booth.base.interceptor.AuthInterceptor;
import com.booth.base.interceptor.UrlInterceptor;
import com.booth.base.model.DataDict;
import com.booth.base.model.Groups;
import com.booth.base.model.GroupsModules;
import com.booth.base.model.ModuleActions;
import com.booth.base.model.Modules;
import com.booth.base.model.Users;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;

public abstract class BaseConfig extends JFinalConfig
{
  public void configConstant(Constants arg0)
  {
    loadPropertyFile("config.properties");
    arg0.setDevMode(getPropertyToBoolean("devMode", Boolean.valueOf(false)).booleanValue());
    arg0.setViewType(ViewType.JSP);
  }

  public void configHandler(Handlers me)
  {
	 
    me.add(new ContextPathHandler("baseUrl"));
  }

  public void configInterceptor(Interceptors arg0)
  {
    arg0.add(new AuthInterceptor());
    System.out.println("bbb=:"+JSON.toJSONString(arg0.getInterceptorArray()));
  }

  public void configPlugin(Plugins me)
  {
    C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), 
      getProperty("user"), getProperty("password"));
    me.add(c3p0Plugin);
    ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
    arp.setShowSql(getPropertyToBoolean("showSql").booleanValue());

    arp.addMapping("groups", Groups.class);
    arp.addMapping("users", "user_id", Users.class);
    arp.addMapping("modules", Modules.class);
    arp.addMapping("module_actions", "id,action_url", ModuleActions.class);
    arp.addMapping("groups_modules", "group_id,modules_id", GroupsModules.class);
    arp.addMapping("data_dict", DataDict.class);
    me.add(arp);
  }

  public void configRoute(Routes arg0)
  {
    arg0.add("/admin", MainController.class, "/WEB-INF/pages/base");
    arg0.add("/admin/groups", GroupsController.class, "/WEB-INF/pages/base");
    arg0.add("/admin/users", UsersController.class, "/WEB-INF/pages/base");
    arg0.add("/admin/modules", ModulesController.class, "/WEB-INF/pages/base");
    arg0.add("/admin/moduleActions", ModuleActionsController.class, "/WEB-INF/pages/base");
    arg0.add("/admin/groupsModules", GroupsModulesController.class, "/WEB-INF/pages/base");
    arg0.add("/admin/dataDict", DataDictController.class, "/WEB-INF/pages/base");
  }
}